package com.example.text.service.serviceImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.dao.AdminUserRoleDao;
import com.example.text.entity.AdminRole;
import com.example.text.entity.AdminRoleMenu;
import com.example.text.entity.AdminUserRole;
import com.example.text.entity.User;
import com.example.text.service.AdminUserRoleService;
import com.example.text.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AdminUserRoleServiceImpl extends ServiceImpl<AdminUserRoleDao, AdminUserRole> implements AdminUserRoleService {

    @Autowired
    AdminUserRoleDao adminUserRoleDao;
    @Autowired
    UserService userService;

    public List<AdminUserRole> listAllByUid(String username) {
        return adminUserRoleDao.getRoleByUserName(username);
    }


    @Transactional
    public void saveRoleChanges(int uid, List<AdminRole> roles) {
        adminUserRoleDao.deleteAllByUid(uid);
        //List<AdminUserRole> urs = new ArrayList<>();
        roles.forEach(r -> {
            AdminUserRole ur = new AdminUserRole();
            ur.setUid(uid);
            ur.setRid(r.getId());
            adminUserRoleDao.insert(ur);
        });

    }
    public  void addRoleByName(String name){

        User u = userService.getByUserName(name);
        AdminUserRole adminUserRole=new AdminUserRole();
        adminUserRole.setUid(u.getId());
        adminUserRole.setRid(3);
        adminUserRoleDao.insert(adminUserRole);
    }

}
